package com.nbsaas.boot.evaluate.rest.resource;

import com.nbsaas.boot.evaluate.api.apis.EvaluateCatalogApi;
import com.nbsaas.boot.entity.evaluate.EvaluateCatalog;
import com.nbsaas.boot.evaluate.api.domain.request.EvaluateCatalogRequest;
import com.nbsaas.boot.evaluate.api.domain.response.EvaluateCatalogResponse;
import com.nbsaas.boot.evaluate.api.domain.simple.EvaluateCatalogSimple;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateCatalogSimpleConvert;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateCatalogEntityConvert;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateCatalogResponseConvert;
import com.nbsaas.boot.evaluate.data.repository.EvaluateCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.evaluate.api.domain.request.EvaluateCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   评价分类-业务接口实现
*/
@Transactional
@Service
public class EvaluateCatalogResource extends BaseResource<EvaluateCatalog,EvaluateCatalogResponse, EvaluateCatalogSimple, EvaluateCatalogRequest>  implements EvaluateCatalogApi {

    @Resource
    private EvaluateCatalogRepository evaluateCatalogRepository;

    @Override
    public JpaRepositoryImplementation<EvaluateCatalog, Serializable> getJpaRepository() {
        return evaluateCatalogRepository;
    }

    @Override
    public Function<EvaluateCatalog, EvaluateCatalogSimple> getConvertSimple() {
        return new EvaluateCatalogSimpleConvert();
    }

    @Override
    public Function<EvaluateCatalogRequest, EvaluateCatalog> getConvertForm() {
        return new EvaluateCatalogEntityConvert();
    }

    @Override
    public Function<EvaluateCatalog, EvaluateCatalogResponse> getConvertResponse() {
        return new EvaluateCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<EvaluateCatalogSimple> list(PageRequest request) {
        EvaluateCatalogSimpleConvert convert=new EvaluateCatalogSimpleConvert();
        if (request instanceof EvaluateCatalogSearch){
             EvaluateCatalogSearch searchRequest=(EvaluateCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<EvaluateCatalogSimple> root(EvaluateCatalogSearch search) {
        ListResponse<EvaluateCatalogSimple> result=new  ListResponse<>();
        List<EvaluateCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


